package com.haizhi.crm.sys.auth.service.impl;

import com.haizhi.crm.common.constant.SysStatus;
import com.haizhi.crm.common.exception.UnexpectedStatusException;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.common.util.Md5SaltUtils;
import com.haizhi.crm.sys.auth.dao.PositionDao;
import com.haizhi.crm.sys.auth.dao.PositionRoleDao;
import com.haizhi.crm.sys.auth.dao.RoleDao;
import com.haizhi.crm.sys.auth.model.PositionDo;
import com.haizhi.crm.sys.auth.model.PositionQo;
import com.haizhi.crm.sys.auth.model.PositionRoleDo;
import com.haizhi.crm.sys.auth.model.RoleDo;
import com.haizhi.crm.sys.auth.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by yangyijun on 2018/5/30.
 */
@Service
@Transactional
public class PositionServiceImpl implements PositionService {

    private static final GLog LOG = LogFactory.getLogger(PositionServiceImpl.class);

    @Autowired
    private PositionDao positionDao;

    @Autowired
    private PositionRoleDao positionRoleDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public Response findAll() {
        try {
            return Response.success(positionDao.list());
        } catch (Exception e) {
            LOG.error("search position list error", e);
            throw new UnexpectedStatusException(SysStatus.FIND_POSITION_LIST_ERROR, e);
        }
    }

    @Override
    public Response save(PositionQo positionQo) {
        try {
            String signature = positionQo.getSignature();
            positionQo.setSignature(null);
            String localSignature = Md5SaltUtils.getEncrypted(positionQo.toString());
            if (localSignature.equals(signature)) {
                //1.upsert position
                upsertPosition(positionQo);
                //2.upsert role
                upsertRole(positionQo);
                //3.upsert position role
                upsertPositionRole(positionQo);
                return Response.success();
            } else {
                throw new UnexpectedStatusException(SysStatus.SYNC_POSITION_ERROR);
            }
        } catch (Exception e) {
            LOG.error("sync position info error", e);
            throw new UnexpectedStatusException(SysStatus.SYNC_POSITION_ERROR, e);
        }
    }

    private void upsertPosition(PositionQo positionQo) {
        PositionDo positionDo = new PositionDo();
        positionDo.setId(positionQo.getId());
        positionDo.setUpdatedDt(new Date());
        positionDo.setName(positionQo.getName());
        positionDo.setEnabledFlag(positionQo.getEnabledFlag());
        PositionDo p = positionDao.findOne(positionQo.getId());
        if (null == p) {
            positionDo.setCreatedDt(new Date());
        }
        positionDao.save(positionDo);
    }

    private void upsertRole(PositionQo positionQo) {
        RoleDo roleDo = new RoleDo();
        roleDo.setId(positionQo.getId());
        roleDo.setName(positionQo.getName());
        roleDo.setRemark(positionQo.getName());
        roleDo.setUpdatedDt(new Date());
        roleDo.setEnabledFlag(positionQo.getEnabledFlag());
        RoleDo r = roleDao.findOne(positionQo.getId());
        if (null == r) {
            roleDo.setCreatedDt(new Date());
        }
        roleDao.save(roleDo);
    }

    private void upsertPositionRole(PositionQo positionQo) {
        PositionRoleDo positionRoleDo = null;
        List<PositionRoleDo> positionRoleDos = positionRoleDao.findByPositionId(positionQo.getId());
        if (CollectionUtils.isEmpty(positionRoleDos)) {
            positionRoleDo = new PositionRoleDo();
            positionRoleDo.setRoleId(positionQo.getId());
            positionRoleDo.setPositionId(positionQo.getId());
            positionRoleDo.setCreatedDt(new Date());
        } else {
            positionRoleDo = positionRoleDos.get(0);
        }
        positionRoleDo.setUpdatedDt(new Date());
        positionRoleDo.setEnabledFlag(positionQo.getEnabledFlag());
        positionRoleDao.save(positionRoleDo);
    }
}
